<?php
/**
 * 用户模型
 * 
 * @package ews
 * @ews Enterprise WorkShop
 * @version 1.0 alpha
 * @since 2011-05-03
 * @author chenyan
 */

class userModel extends model
{
	
	/**************************************************************************
	 * 返回 值说明
	 * 1.返回的数组（user）说明
	 * 		user_id				用户ID				(int)
  			password			用户密码				(string,密文)
  			username			用户真实姓名			(string)
  			email				用户邮箱				(string)
 			unread_notice		未读消息				(int)
  			flag_id				权限	ID				(int)
  			reg_time 			注册时间				(时间戳)
  			doing				当前状态				(string)
  			theme				个性化设置			(string)
	 * 
	 * 2.若返回值为操作是否成功	1：成功      0或则负数：失败
	 **************************************************************************/
	
	
	/**
	 * 创建新用户
	 * 
	 * @param string $email			邮箱地址
	 * @param string $password		密码
	 * @param string $username		用户名
	 * @param string $flag_id		权限ID
	 * @return 新插入用户ID
	 */
	public function add($email, $password, $username,$flag_id){
		if(!preg_match('/^[a-z]([a-z0-9]*[-_\.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?$/i', $email)){
			return -1;	//email地址有误
		}
		$password = $this->password($password);
		$data = array('email' => $email, 'password' => $password, 'username' => $username,'flag_id'=>$flag_id,
				'unread_notice'=>0,'reg_time'=>time(),'doing'=>'','theme'=>'default');
		$user_id = DB::insert('user', $data);
		//向mailer表中插入空数据 
		$data_mailer =array('user_id'=>$user_id,'email'=>'','login_name'=>'','login_password'=>'',
					'mailer_type'=>'','pop_type'=>'','pop_server'=>'','pop_port'=>0,
					'smtp_server'=>'','smtp_port'=>0,'is_ssl'=>0);
		DB::insert('mailer', $data_mailer);
		return $user_id;
	}
	
	/**
	 * 删除用户
	 * @param int $user_id 	用户ID
	 * @return  			1:成功  0:失败
	 */
	public function delete($user_id){
		$condition = "user_id=$user_id";
		$row = DB::delete('user',$condition);
		if($row>0) return 1;
		else return 0;
	}
	
	/**
	 * 用户资料更新
	 * 
	 * @param int $user_id
	 * @param string $username 用户名
	 * @param string $email 邮箱
	 * @param string $password 密码，为空则不修改
	 * @param int $flag_id 权限ID
	 * @return void
	 * 
	 * @author monkee
	 */
	public function update($user_id, $email, $username, $password, $flag_id){
		if(empty($password)){
			DB::update('user', array('username' => $username, 'email' => $email, 'flag_id' => $flag_id), 'user_id = '.$user_id);
		}else{
			DB::update('user', array('username' => $username, 'email' => $email, 'flag_id' => $flag_id, 'password'=> $this->password($password)), 'user_id = '.$user_id);
		}
	}
	/**
	 * 修改用户名
	 * @param int $user_id 用户ID
	 * @return  int				1:成功  0:失败
	 */
	public function set_username($user_id,$username){
		$data = array('username' => $username);
		$condition = "user_id=$user_id";
		$row = DB::update('user',$data,$condition);
		if($row>0) return 1;
		else return 0;
	}
	
	/**
	 * 修改用户密码
	 * @param int $user_id 用户ID
	 * @param string $password 新密码
	 * @return  int				1:成功  0:失败
	 */
	public function set_password($user_id,$password){
		$password = $this->password($password);
		$data = array('password' => $password);
		$condition = "user_id=$user_id";
		$row = DB::update('user',$data,$condition);
		if($row>0) return 1;
		else return 0;
	}

	/**
	 * 设置用户个性化
	 * @param int $user_id 	用户ID
	 * @param int $theme	风格
	 * @return  int				1:成功  0:失败
	 */
	public function set_theme($user_id,$theme){	
		$data = array('theme' => $theme);
		$condition = "user_id=$user_id";
		$row = DB::update('user',$data,$condition);
		if($row>0) return 1;
		else return 0;
	}
	
	/**
	 * 设置用户权限组
	 * @param int $user_id	用户ID
	 * @param int $flag_id	风格
	 * @return  int				1:成功  0:失败
	 */
	public function set_flag($user_id,$flag_id){
		$data = array('flag_id' => $flag_id);
		$condition = "user_id=$user_id";
		$row = DB::update('user',$data,$condition);
		if($row>0) return 1;
		else return 0;
	}
	
	/**
	 * 根据用户ID获取用户信息
	 * @param int $user_id		用户ID
	 * 
	 * @return 一维数组
	 */
	public function get_user_by_user_id($user_id){
		$row = DB::fetchOne('SELECT * FROM '.DB::table('user').' WHERE user_id = '.DB::escape_string($user_id));
		if(count($row) == 0){
			return array();
		}
		return $row;
	}
	
	/**
	 * 根据用户id获取其后面的用户数组
	 * @param int $user_id	用户ID
	 * @param int $count	获取数
	 * 
	 * @return 二维数组
	 */
	public function get_users_by_pro_id($user_id,$count){
		$r = DB::fetchArray("SELECT * FROM ".DB::table('user')." WHERE user_id>$user_id limit $count");
		return $r;
	}
	
	/**
	 * 根据用户id获取其后面的用户数组
	 * @param int $user_id	用户ID
	 * 
	 * @return int 结果数
	 */
	public function get_users_count_by_pro_id($user_id){
		$row = DB::fetchOne("SELECT count(*) as num FROM ".DB::table('user')." WHERE user_id>$user_id");
		return $row['num'];
	}
	
	/**
	 * 分页获取用户列表
	 * @param int $pagesize 每页大小
	 * @param int $pagenum  页号
	 * 
	 * @return 二维数组
	 */
	public function get_users($pagesize,$pagenum){
		if($pagesize<=0 || $pagenum<=0) return array();
		$row = DB::fetchOne("SELECT count(*) as num FROM ".DB::table('user'));
		$totalcount = $row['num'];
		if($totalcount==0) return array();
		//分页获取
		$totalpage = ceil($totalcount / $pagesize);
		$pagenum = min($totalpage, $pagenum);
		$limitstart = ($pagenum-1)*$pagesize;
		$r = DB::fetchArray("SELECT * FROM ".DB::table('user')." limit $limitstart,$pagesize");
		return $r;
	}
	
	/**
	 * 获取所有用户数
	 * 
	 * @return int
	 */
	public function get_user_count(){
		$row = DB::fetchOne("SELECT count(*) as num FROM ".DB::table('user'));
		return $row['num'];
	}

	/**
	 * 根据用户名（非模糊）分页获取用户列表
	 * @param string $username 用户名
	 * @return 二维数组
	 */
	public function get_users_by_username($username,$pagesize,$pagenum){
		if($pagesize<=0 || $pagenum<=0) return array();
		$row = DB::fetchOne("SELECT count(*) as num FROM ".DB::table('user')." where username='$username'");
		$totalcount = $row['num'];
		if($totalcount==0) return array();
		//分页获取
		$totalpage = ceil($totalcount / $pagesize);
		$pagenum = min($totalpage, $pagenum);
		$limitstart = ($pagenum-1)*$pagesize;
		$r = DB::fetchArray("SELECT * FROM ".DB::table('user')." where username='$username'"." limit $limitstart,$pagesize");
		return $r;
	}
	
	/**
	 * 根据用户名（非模糊）获取用户数
	 * @param string $username 用户名
	 * 
	 * @return int
	 */
	public function get_user_count_by_username($username){
		$row = DB::fetchOne("SELECT count(*) as num FROM ".DB::table('user')." where username='$username'");
		return $row['num'];
	}
	
	/**
	 * 根据用户ID获得其权限信息
	 * @param int $user_id 	用户ID
	 * 
	 * @return 一维数组，
	 * 		       若数组为空，表示无结果
	 * 		       否则，数组结构为
	 * 		  flag_id					权限ID						(int)
	 * 		  name						权限名称						(string)
	 * 		  flag_user_edit			添加/编辑/删除用户权限		(int)
	 * 		  flag_flag_edit			添加/编辑/删除用户组权限		(int)
	 *	 	  flag_group_create			添加小组权限					(int)
	 * 		  flag_knowledge_create		添加知识权限					(int)
	 * 		  flag_knowledge_delete		删除知识权限					(int)
	 * 		  flag_work_create			创建工作权限					(int)
	 * 		  flag_todo_create			创建待办权限					(int)
	 */
	public function get_flag_by_user_id($user_id){
		$q="SELECT f.* FROM ".DB::table('user')." as u,".DB::table('flag')." as f "
			." WHERE u.flag_id=f.flag_id AND u.user_id = $user_id";
		$row = DB::fetchOne($q);
		if(count($row) == 0){
			return array();
		}
		return $row;
	}
	/**
	 * 用户登陆
	 * 
	 * @param string $email
	 * @param string $password
	 * @return · 1：成功
	 * 			-1：email地址有误
	 * 			-2：用户不存在
	 * 			-3：密码错误
	 */
	public function login($email, $password){
		if(!preg_match('/^[a-z]([a-z0-9]*[-_\.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?$/i', $email)){
			return -1;	//email地址有误
		}
		$password = $this->password($password);
		$row = DB::fetchOne('SELECT * FROM '.DB::table('user').' WHERE email = "'.DB::escape_string($email).'"');
		if(count($row) == 0){
			return -2;	//用户不存在
		}
		if($row['password'] != $password){
			return -3;	//密码错误
		}
		//设置COOKIE
		Cookie::set('user_id', $row['user_id']);
		Cookie::set('username', $row['username']);
		return 1;
	}
	
	/**
	 * 注销用户
	 * @param int $user_id 用户ID
	 * 
	 * @return 1:成功
	 */
	public function logout($user_id){
		Cookie::set('user_id', null);
		Cookie::set('username', null);
		return 1;
	}
	
	/**
	 * 检查用户名是否设置过
	 * @param int $user_id $user_id 用户ID
	 * 
	 * @return 1:设置过 0：未设置过
	 */
	public function username_is_seted($user_id){
		$row = DB::fetchOne("SELECT username FROM ".DB::table('user')." where user_id=$user_id");
		if(empty($row['username'])||$row['username']==''){
			return 0;
		}
		
		return 1;
	}
	
	/**
	 * 用户密码加密
	 * 
	 * @param string $password
	 */
	public function password($password){
		return md5($password);
	}
	
	public function get_all_users(){
		return DB::fetchArray('SELECT user_id, username FROM '.DB::table('user'));
	}
	
	
	
	
	/*********************************************待实现方法*********************************************/

}
?>